<?php

	/*
	 *	obsluga tworzenia listy stron - tzw. paginacja
	 *
	 */

	class CPageList
	{
		static function Create($iTotalRows,$iRowsPerPage,$iCurrPage = 0,$szUrl = '',&$iDbSkipOffset = 0)
		{
			$iTotalRows   = (int) $iTotalRows;	//totoal row count
		
			$iRowsPerPage = (int) $iRowsPerPage;	//how many rows per page
			
			$iPageWindow  = 3; 			// page sliding window size 
			
			$iTotalPages  = 0;			//total pages count
			
			$aNavigation  = array();		//navigation window content

			//let's obtain current page
			$iCurrPage = isset($_GET['page']) ? (int) $_GET['page'] : (int) $iCurrPage;
			
			//what about url
			$szUrl = ($szUrl != '') ? (string)$szUrl : substr($_SERVER['REQUEST_URI'],0,intval(MAX_URL_LEN));

				
			$iTotalPages = (int) ceil($iTotalRows/$iRowsPerPage); 
			
			//if all rows have enough space in one page
			if($iTotalRows == 0 || $iTotalPages <= 1) { $iDbSkipOffset = 0;  return array(); }
			
			
			if($iCurrPage <= 0)
			{ 
				$iCurrPage = 1; 
			}
			else if($iCurrPage > $iTotalPages)
			{
				$iCurrPage = $iTotalPages;
			}
			
			//calculating database skip offset
			$iDbSkipOffset = $iRowsPerPage*($iCurrPage-1);
			
			
			//dealing with url
			if( strpos($szUrl,'page') )
			{
				$szUrl = eregi_replace('page=[_a-z0-9-]+','',$szUrl);
			}
			else
			{
				$szUrl = $szUrl.(strpos($szUrl,'?') ? '&' : '?' );
			}
			
			
			//creating backword navigation entry
				if($iCurrPage != 1)
			{
				 $aNavigation[] = array('no' => '&laquo Wstecz', 'url' => $szUrl.'page='.($iCurrPage-1), 'curr' => 0);
			}
								 
			if($iTotalPages <= $iPageWindow)
			{
				
				for($i=1; $i<$iTotalPages+1; $i++)
				{
					if($i == $iCurrPage)
					{
						$aNavigation[] = array('no' => $i, 'url' => $szUrl.'page='.$i, 'curr' => 1);
					}
					else
					{
						$aNavigation[] = array('no' => $i, 'url' => $szUrl.'page='.$i, 'curr' => 0);
					}
				}
			}
			else
			{
				//window begin & end page
					$iWindowStart = $iCurrPage - $iPageWindow;
					$iWindowEnd   = $iCurrPage + $iPageWindow;
							
				//window size checkup
				if($iWindowStart <= 0) { $iWindowStart = 1; }
										
				if($iWindowEnd >= $iTotalPages) { $iWindowEnd = $iTotalPages; }
				
				//where link to the first page should appear
					if($iCurrPage > $iPageWindow)
					{
					$aNavigation[] = array('no' => 1, 'url' => $szUrl.'page=1', 'curr' => 0);
						
					$aNavigation[] = array('no' => '...', 'url' => '', 'curr' => 0);
				}
				
				for($i=$iWindowStart; $i<$iWindowEnd+1; $i++)
				{
					if($i == $iCurrPage)
					{
						$aNavigation[] = array('no' => $i, 'url' => $szUrl.'page='.$i, 'curr' => 1);
					}
					else
					{
						$aNavigation[] = array('no' => $i, 'url' => $szUrl.'page='.$i, 'curr' => 0);
					}
				}
				
				if($iCurrPage < $iTotalPages - $iPageWindow)
				{
					$aNavigation[] = array('no' => '...', 'url' => '', 'curr' => 0);
						 
					$aNavigation[] = array('no' => $iTotalPages, 'url' => $szUrl.'page='.$iTotalPages, 'curr' => 0);
				}							     																				    																	
			}																							    
		
			//creating forward navigation entry
			if($iCurrPage != $iTotalPages)
			{
				$aNavigation[] = array('no' => 'Dalej &raquo', 'url' => $szUrl.'page='.($iCurrPage+1), 'curr' => 0);
			}
							
			
			return $aNavigation;
		}
	}

?>